﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AirProperties.Resources;

namespace AirProperties
{
    public partial class LocaleManager : Form
    {
        //the default locales supported by the AIR application installer
        private ListItem[] defaultLocales = { new ListItem(LocaleHelper.GetString("Locale.ChineseSimplified"), "zh"),
                                                new ListItem(LocaleHelper.GetString("Locale.ChineseTraditional"), "zh-Hant"),                                                
                                                new ListItem(LocaleHelper.GetString("Locale.Czech"), "cs"),
                                                new ListItem(LocaleHelper.GetString("Locale.Dutch"), "nl"),
                                                new ListItem(LocaleHelper.GetString("Locale.English"), "en"),
                                                new ListItem(LocaleHelper.GetString("Locale.French"), "fr"),
                                                new ListItem(LocaleHelper.GetString("Locale.German"), "de"),
                                                new ListItem(LocaleHelper.GetString("Locale.Italian"), "it"),
                                                new ListItem(LocaleHelper.GetString("Locale.Japanese"), "jp"),
                                                new ListItem(LocaleHelper.GetString("Locale.Korean"), "ko"),
                                                new ListItem(LocaleHelper.GetString("Locale.Polish"), "pl"),
                                                new ListItem(LocaleHelper.GetString("Locale.Portuguese"), "pt-BR"),
                                                new ListItem(LocaleHelper.GetString("Locale.Russian"), "ru"),
                                                new ListItem(LocaleHelper.GetString("Locale.Spanish"), "es"),
                                                new ListItem(LocaleHelper.GetString("Locale.Swedish"), "sv"),
                                                new ListItem(LocaleHelper.GetString("Locale.Turkish"), "tr")
                                            };
        private List<string> applicationLocales;


        public LocaleManager(ref List<string> applicationLocales)
        {
            InitializeComponent();
            this.applicationLocales = applicationLocales;
            LoadDefaultLocales();
        }


        private void LoadDefaultLocales()
        {
            Boolean isDefaultLocale;

            //add defaults to available list
            lbAvailable.Items.AddRange(defaultLocales);
            //check each selected against defaults
            foreach (String locale in applicationLocales)
            {
                isDefaultLocale = false;
                foreach (ListItem defaultLocale in defaultLocales)
                {
                    if (locale.ToLower().Equals(defaultLocale.Value.ToLower()))
                    {
                        lbSelected.Items.Add(defaultLocale);
                        lbAvailable.Items.Remove(defaultLocale);
                        isDefaultLocale = true;
                        break;
                    }
                }
                if (!isDefaultLocale)
                {
                    lbSelected.Items.Add(new ListItem(locale, locale));
                }
            }
        }

        private void AddDefaultLocale()
        {
            ListItem selectedLocale;

            if (lbAvailable.SelectedIndex >= 0)
            {
                selectedLocale = (ListItem)lbAvailable.SelectedItem;
                lbSelected.Items.Add(selectedLocale);
                applicationLocales.Add(selectedLocale.Value);
                lbAvailable.Items.Remove(selectedLocale);
            }
        }

        private void RemoveLocale()
        {
            ListItem selectedLocale;

            if (lbSelected.SelectedIndex >= 0)
            {
                selectedLocale = (ListItem)lbSelected.SelectedItem;
                lbSelected.Items.Remove(selectedLocale);
                applicationLocales.Remove(selectedLocale.Value);
                //only re-add default locales back to the available list
                foreach (ListItem defaultLocale in defaultLocales)
                {
                    if (selectedLocale.Name.Equals(defaultLocale.Name))
                    {
                        lbAvailable.Items.Add(selectedLocale);
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            String newLocale = tbCustomLocale.Text.Trim();

            if (newLocale.Length > 0)
            {
                if (!applicationLocales.Contains(newLocale))
                {
                    lbSelected.Items.Add(new ListItem(newLocale, newLocale));
                    applicationLocales.Add(newLocale);
                }
            }
            tbCustomLocale.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddDefaultLocale();
        }

        private void lbAvailable_DoubleClick(object sender, EventArgs e)
        {
            AddDefaultLocale();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            RemoveLocale();
        }

        private void lbSelected_DoubleClick(object sender, EventArgs e)
        {
            RemoveLocale();
        }
        
        
        //Simple validation to check if new locale is an extension of a default locale
        private void tbCustomLocale_Validating(object sender, CancelEventArgs e)
        {
            String newLocale = tbCustomLocale.Text.Trim();
            String baseLocale;
            Boolean isValid = false;

            if (newLocale.Length > 0)
            {
                foreach (ListItem locale in defaultLocales)
                {
                    baseLocale = locale.Value + "-";
                    if (newLocale.StartsWith(baseLocale) && newLocale.Length > baseLocale.Length)
                    {
                        isValid = true;
                        break;
                    }
                }
                if (!isValid)
                {
                    errorProvider1.SetError(tbCustomLocale, LocaleHelper.GetString("Validation.InvalidLocale"));
                    e.Cancel = true;
                }
                else
                    errorProvider1.SetError(tbCustomLocale, "");
            }
        }

        
    }
}
